package com.xmkj.bearbiz.mine.activity;

import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.common.base.Config;
import com.common.carbean.ThreePayBean;
import com.common.mvp.BaseMvpActivity;
import com.common.mvp.BasePresenter;
import com.common.retrofit.methods.ChongZhiMethods;
import com.common.retrofit.subscriber.CommonSubscriber;
import com.common.retrofit.subscriber.SubscriberListener;
import com.common.rxbus.RxBus;
import com.common.rxbus.postevent.RxKeyEvent;
import com.common.utils.EmptyUtils;
import com.xgr.easypay.EasyPay;
import com.xgr.easypay.alipay.AliPay;
import com.xgr.easypay.alipay.AlipayInfoImpli;
import com.xgr.easypay.callback.IPayCallback;
import com.xgr.easypay.wxpay.WXPay;
import com.xgr.easypay.wxpay.WXPayInfoImpli;
import com.xmkj.bearbiz.R;
import com.xmkj.payandlogin.ShareConfig;
import com.xmkj.payandlogin.ShareManager;

import java.util.List;

/**
 * Create by leo
 * 创建日期：2018/4/9
 * 描述：
 */

public class ChongZhiActivity extends BaseMvpActivity {
    TextView tvPay;
    RadioButton rb1;
    RadioButton rb2;
    RadioGroup radioGroup ;
    EditText et_money;
    String payType = "1";

    @Override
    protected BasePresenter createPresenterInstance() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_chong_zhi;
    }

    @Override
    protected void onViewCreated() {
        tvPay = findViewById(R.id.tv_pay);
        rb1 = findViewById(R.id.rb1);
        rb2 = findViewById(R.id.rb2);
        et_money = findViewById(R.id.et_money);
        radioGroup = findViewById(R.id.radio_group);
    }

    @Override
    protected void doLogicFunc() {
        attachClickListener(tvPay);
        ShareConfig config = ShareConfig.instance()
                .qqId(Config.APP_QQ_ID)
                .wxSecret(Config.APP_SERECET)
                .wxId(Config.APP_ID);
        ShareManager.init(config);
        radioGroup.check(rb2.getId());
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

                switch (checkedId) {
                    case R.id.rb1:
                        payType = 2+"";  // 微信
                        break;
                    case R.id.rb2:
                        payType = 1+"";  //支付宝
                        break;
                }
            }
        });
    }

    @Override
    protected void onViewClicked(View view) {
        if (view.getId() == tvPay.getId()) {
            if (EmptyUtils.isEmpty(getEditTextStr(et_money))||Integer.valueOf(getEditTextStr(et_money))==0) {
                showToastMsg("请输入金额");
                return;
            }
            gotoChongzhi();
        }
    }

    private void gotoChongzhi() {
        showProgressingDialog();
        CommonSubscriber<ThreePayBean> subscriber = new CommonSubscriber<>(new SubscriberListener() {
            @Override
            public void onNext(Object o) {
                ThreePayBean infoBean = (ThreePayBean) o;
                dismissProgressDialog();
                if(EmptyUtils.isEmpty(infoBean.alipay)){
                    wxpayYj(infoBean.wxpay);
                }else {
                    alipayYJ(infoBean.alipay);
                }



            }

            @Override
            public void onError(String e, int code) {
                dismissProgressDialog();
                showToastMsg(e);
            }
        });

        ChongZhiMethods.getInstance().pay_recharge_order(subscriber,uid,hashid,payType,getEditTextStr(et_money));
        rxManager.add(subscriber);
    }
    private void wxpayYj(List<ThreePayBean.WxpayEntity> bean) {
        //实例化微信支付策略
        String wxAppId = Config.APP_ID;
        WXPay wxPay = WXPay.getInstance((Activity) context, wxAppId);
        //构造微信订单实体。一般都是由服务端直接返回。
        WXPayInfoImpli wxPayInfoImpli = new WXPayInfoImpli();
        wxPayInfoImpli.setTimestamp(bean.get(0).timestamp + "");
        wxPayInfoImpli.setSign(bean.get(0).sign);
        wxPayInfoImpli.setPrepayId(bean.get(0).prepayid);
        wxPayInfoImpli.setPartnerid(bean.get(0).partnerid);
        wxPayInfoImpli.setAppid(Config.APP_ID);
        wxPayInfoImpli.setNonceStr(bean.get(0).noncestr);
        wxPayInfoImpli.setPackageValue(bean.get(0).packageX);
        //策略场景类调起支付方法开始支付，以及接收回调。
        EasyPay.pay(wxPay, (Activity) context, wxPayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                showToastMsg("支付成功");
                onBackPressed();
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.MODIFYSUCCESS, true));
            }

            @Override
            public void failed() {
                showToastMsg("支付失败");

            }

            @Override
            public void cancel() {
                showToastMsg("支付取消");
            }
        });

    }

    private void alipayYJ(String payInfo) {
        //实例化支付宝支付策略
        AliPay aliPay = new AliPay();
        //构造支付宝订单实体。一般都是由服务端直接返回。
        AlipayInfoImpli alipayInfoImpli = new AlipayInfoImpli();
        alipayInfoImpli.setOrderInfo(payInfo);
        //策略场景类调起支付方法开始支付，以及接收回调。
        EasyPay.pay(aliPay, (Activity) context, alipayInfoImpli, new IPayCallback() {
            @Override
            public void success() {
                showToastMsg("支付成功");
                onBackPressed();
                RxBus.getDefault().postSticky(new RxKeyEvent(RxKeyEvent.MODIFYSUCCESS, true));
//                onBackPressed();
            }

            @Override
            public void failed() {
                showToastMsg("支付失败");
            }

            @Override
            public void cancel() {
                showToastMsg("支付取消");
            }
        });
    }
    @Override
    protected void setNavigation() {
        setNavigationBack("充值");
    }
}
